package com.yupi.usercenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.usercenter.common.BaseResponse;
import com.yupi.usercenter.common.ErrorCode;
import com.yupi.usercenter.common.ResultUtils;
import com.yupi.usercenter.exception.BusinessException;
import com.yupi.usercenter.model.domain.Team;
import com.yupi.usercenter.model.domain.User;
import com.yupi.usercenter.model.domain.request.TeamAddRequest;
import com.yupi.usercenter.model.domain.request.TeamJoinRequest;
import com.yupi.usercenter.model.domain.request.TeamUpdateRequest;
import com.yupi.usercenter.model.domain.vo.TeamUserVO;
import com.yupi.usercenter.model.dto.TeamQuery;
import com.yupi.usercenter.service.TeamService;
import com.yupi.usercenter.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/team")
//@CrossOrigin(origins =
//        {"http://localhost:5173","http://127.0.0.1:5173"},allowCredentials = "true")
@Slf4j

public class TeamController {
    @Autowired
    private UserService userService;
    @Autowired
    private TeamService teamService;

//    @PostMapping("/add")
//    public BaseResponse<Long> addTeam(@RequestBody Team team){
//        if(team == null){
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        boolean save = teamService.save(team);
//        if(!save){
//            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"插入失败");
//
//        }
//        return ResultUtils.success(team.getId());
//    }
    @PostMapping("/delete")
    public BaseResponse<Boolean> delete(long id){
        if(id<=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean delete = teamService.removeById(id);
        if(!delete){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"删除失败");

        }
        return ResultUtils.success(true);

    }
//    @PostMapping("/update")
//    public BaseResponse<Boolean> updateTeam(@RequestBody Team team){
//        if (team == null){
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        boolean result = teamService.updateById(team);
//        if (!result){
//            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"修改失败");
//        }
//        return ResultUtils.success(true);
//
//    }
    @GetMapping("/get")
    public BaseResponse<Team> getTeamById(long id){
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Team team = teamService.getById(id);
        if (team == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        return ResultUtils.success(team);
    }
//    @GetMapping("/list")
//    public BaseResponse<List<Team>> listTeams(TeamQuery teamQuery){
//        if (teamQuery == null){
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        Team team = new Team();
//        BeanUtils.copyProperties(team,teamQuery);
//        QueryWrapper<Team> queryWrapper = new QueryWrapper<>();
//        List<Team> teamList = teamService.list(queryWrapper);
//        return ResultUtils.success(teamList);
//    }
    @GetMapping("/list/page")
    public BaseResponse<Page<Team>> listTeamsByPage(TeamQuery teamQuery){
        if (teamQuery == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Team team = new Team();
        BeanUtils.copyProperties(teamQuery,team);
        Page<Team> page = new Page<>(teamQuery.getPageNum(),teamQuery.getPageSize());
        QueryWrapper<Team> queryWrapper = new QueryWrapper<>(team);
        Page<Team> resultPage = teamService.page(page, queryWrapper);
        return ResultUtils.success(resultPage);
    }

//    @PostMapping("/add")
//    public BaseResponse<Long> addTeam(@RequestBody TeamAddRequest teamAddRequest,
//                                      HttpServletRequest request){
//        if (teamAddRequest == null){
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        User loginUser = userService.getLogininUser(request);
//        Team team = new Team();
//        BeanUtils.copyProperties(teamAddRequest,team);
//        long teamId = teamService.addTeam(team, loginUser);
//        return ResultUtils.success(teamId);
//    }
       @PostMapping("/add")
       public BaseResponse<Long> addTeam(@RequestBody TeamAddRequest teamAddRequest, HttpServletRequest request){
        if (teamAddRequest == null){
           throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        User logininUser = userService.getLogininUser(request);
        Team team = new Team();
        BeanUtils.copyProperties(teamAddRequest,team);
        long teamId = teamService.addTeam(team,logininUser);
        return ResultUtils.success(teamId);
        }
        @GetMapping("/list")
          public BaseResponse<List<TeamUserVO>> listTeams(TeamQuery teamQuery,HttpServletRequest request){
        if (teamQuery!=null){
            boolean isAdmin = userService.isAdmin(request);
            List<TeamUserVO> teamList =teamService.listTeams(teamQuery,isAdmin);
            return ResultUtils.success(teamList);
        }
        throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
      @PostMapping("/update")
       public BaseResponse<Boolean> updateTeam(@RequestBody TeamUpdateRequest teamUpdateRequest, HttpServletRequest request){
        if (teamUpdateRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLogininUser(request);
        boolean result = teamService.updateTeam(teamUpdateRequest,loginUser);
        if (!result){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"修改失败");
        }
        return ResultUtils.success(true);
    }
    @PostMapping("/join")
    public BaseResponse<Boolean> joinTeam(@RequestBody TeamJoinRequest teamJoinRequest,HttpServletRequest request){
        if (teamJoinRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLogininUser(request);
        boolean result = teamService.joinTeam(teamJoinRequest,loginUser);
        return ResultUtils.success(result);
    }




}
